import os
from bs4 import BeautifulSoup
import urllib.request
import re
import html2text as ht
# os.system('pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests==2.24.0')
# # 检查库文件，没有就自动安装
# os.system('cls')

import requests



headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
    'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
}

cookies = {
    'cookie': "file_1713498_readed=""; file_126955_readed=""; file_286452_readed=""; csrftoken=URjPK1kk4wejYwaXVrGdQw7qhyiU76qK3411rMZVluku3k1QW5Nqui6Vg6olIkRp; sessionid=qrgm3h1tooe4p8niny7frcndraxdzgsp"
# 这里要重新改一下，防止我的cookie泄露我删除了几个字符
# 这里要重新复制为你自己的cookie
}

url_tmp = 'https://www.acwing.com/problem/content/'
url = url_tmp

for i in range(1, 20):
    url = url_tmp + str(i)
    r = requests.get(url=url, headers=headers, cookies=cookies) # 请求网址
    soup = BeautifulSoup(str(r.text), 'html.parser')   #文档对象
    # print(soup.title.get_text())
    title = soup.title.get_text()
    l = len(title);
    # print();
    # print(l);
    text_maker = ht.HTML2Text()
    text_maker.bypass_tables = False
    if (len(soup.find_all('div',class_='ui bottom attached tab active martor-preview')) >= 1):
        htmlpage = str(soup.find_all('div',class_='ui bottom attached tab active martor-preview')[0])
        text = text_maker.handle(htmlpage)
        print(title[0:l - 10])
        s = "# " + title[0:l - 10] + "\n\n#### 题目描述\n\n"
        s += text
        open(title + ".md", "w").write(s)


